﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4rumDCT.App_Code;
using System.Web.Security;
using QLTB.Entities;
using QLTB.Data;

namespace QuanLyThietBi.DCTEDU_
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie cookie = Request.Cookies.Get("NguoiDung");
                if (Request.Cookies["NguoiDung"] != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(cookie);
                }
            }
        }

        protected void cmdDangNhap_Click(object sender, EventArgs e)
        {
            string strUserName = Common.StandardString(txtTenTruyCap.Text);
            string strPassWord = Common.StandardString(txtMatKhau.Text);
            string strUserCode = strUserName + strPassWord;
            strPassWord = Common.EncryptMD5(strPassWord);
            strUserCode = Common.EncryptMD5(strUserCode);
            DoLogin(strUserName, strPassWord, strUserCode);

        }

        public void DoLogin(string strUserName, string strPassword, string strUserCode)
        {
            using (NguoiDung entNguoiDung = DataRepository.NguoiDungProvider.GetByTenTruyCap(strUserName))
            {
                if (entNguoiDung == null)
                {
                    CanBo entCanBo = DataRepository.CanBoProvider.GetByTenTruyCap(strUserName);
                    if (entCanBo == null)
                    {
                        WebMessage.Show("Người dùng không tồn tại trong hệ thống, Xin vui lòng thử lại");
                        txtTenTruyCap.Focus();
                        return;
                    }
                    else
                    {
                        if (entCanBo.MatKhau == txtMatKhau.Text)
                        {
                            WriteCookie();
                            HttpCookie cookie = Request.Cookies.Get("UserNameAndPassword");
                            string strUserInfor = string.Format("{0};{1};{2}", entCanBo.MaCanBo, entCanBo.TenTruyCap, entCanBo.HoTen);
                            FormsAuthentication.SetAuthCookie(strUserInfor, false);
                            Response.Redirect("~/DCTEDU_/HomeCanBo.aspx");
                        }
                        else
                        {
                            WebMessage.Show("Mật khẩu không đúng, Xin vui lòng thử lại");
                            return;
                        }
                    }
                }
                string CurrentPassword = entNguoiDung.MatKhau;
                string CurrentUserCode = entNguoiDung.MaTruyCap;
                string strPassWord = Common.StandardString(txtMatKhau.Text);
                if (strPassword.Equals(CurrentPassword) && strUserCode.Equals(CurrentUserCode))
                {
                    string strUserInfor = string.Format("{0};{1};{2}", entNguoiDung.Id, entNguoiDung.TenTruyCap, entNguoiDung.HoTen);
                    FormsAuthentication.SetAuthCookie(strUserInfor, false);
                    Response.Redirect("~/DCTEDU_/HomeAdmin.aspx");
                }
                else
                {
                    WebMessage.Show("Mật khẩu không đúng, Xin vui lòng thử lại");
                    txtMatKhau.Focus();
                    return;
                }
            }


        }

        protected void WriteCookie()
        {

            HttpCookie cookie = new HttpCookie("DangNhap", "yes");
            cookie.Expires = DateTime.Now.AddHours(12);
            Response.Cookies.Add(cookie);
            cookie = new HttpCookie("UserNameAndPassword");
            cookie.Values.Add("UserName", txtTenTruyCap.Text.Trim());
            cookie.Values.Add("Password", txtMatKhau.Text.Trim());
            Response.Cookies.Add(cookie);
        }
    }
}